/*
 * @Description: 修改附件
 */
import { useBaseInfoWithOut } from '@/store/modules/base-info';
import { createAttachment } from '@/helper/relation-doc';
import globalData from '@/plugins/data';
import globalMethods from '@/plugins/methods';
import emitter from '@/plugins/global-callback';

const useBaseInfo = useBaseInfoWithOut();

//生成必要数据
function getAttActionLog(summaryId) {
    //获取相关文档和附件列表数据
    const { attachments, relationDocs } = useBaseInfo;
    //声明老数据的列表
    const attachmentList = new ArrayList();

    createAttachment(attachments.atts, 'showAttFile')
        .concat(createAttachment(relationDocs.atts, 'Doc1'))
        .forEach((d) => {
            d.createDate = globalMethods.getDateString(d.createDate);
            return attachmentList.add(d);
        });
    //添加进老数据
    return new AttActionLog(summaryId, summaryId, null, attachmentList);
}

function updateAttachment() {
    const {
        summary: { id: summaryId },
        currentUser: {id: userId}
    } = useBaseInfo.businessData;
    //添加锁
    const res = window.callBackendMethod("colManager", "writeLock", summaryId);
    //获取锁失败且锁不属于自己
    if (res?.success !== true && res?.lockInfo?.owner !== userId) {
        globalMethods.warning({
            title: globalMethods.i18n('common.system.hint.label'),
            content: res.msg,
        });
        return;
    }
    const data = {
        // isForwardCol: isForward,
        attActionLog: getAttActionLog.call(this, summaryId),
    };

    //有密级就加进去
    const secretLevel = useBaseInfo.baseInfo.secretLevel;
    const secretLevelAble = !secretLevel.hidden || false;
    const url = `${globalData.ctxPath}/attachmentController.do?method=updateAttachment&category=1&reference=${summaryId}&secretLevelAble=${secretLevelAble}&secretLevel=${secretLevel?.value||''}&subReference=${summaryId}&isSetSecretLevelSure=true`;
    if (!secretLevel.hidden) {
        const secretLevels = {};
        secretLevel.options.forEach(({ value, name }) => {
            if (value) {
                secretLevels[value] = name;
            }
        });
        data.secretLevels = JSON.stringify(secretLevels);
        data.secretLevelAble = 'true';
    }

    console.log(data.attActionLog);

    const dialog = globalMethods.dialog({
        url,
        width: 550,
        height: 400,
        bodyStyle:'padding:0',
        title: this.$i18n('common.toolbar.updateAttachment.label'),
        transParams: {
            parentWin: data,
            ...data,
        },
        onCancel() {
            dialog.close();
        },
        onConfirm: () => {
            const [{ instance },{instance:logList}] = dialog.getValue();
            console.log(instance, logList);
            if (!logList) {
                logList.forEach(log => log.desc = log.des)
            }
            const attachmentList = [];
            const relationDocsList = [];
            //设置回附件和关联文档
            instance.forEach((d) => {
                d.createdate = d.createDate;
                d.canFavorite = d.canFavourite;
                d.hasSaved = true;

                const oldFileItem = useBaseInfo.attachments.atts.find(i => i.id === d.id);
                //新增的附件不支持批量下载、查看、预览、收藏
                if (!oldFileItem){
                    d.hasFavorite = false;
                    d.canFavorite = false;
                    d.hasSaved = false; 
                }else {
                    //附件存在,只能修改附件的密级和名称
                    const {secretLevel, secretLevelName, filename, fileUrl2} = d;
                    Object.assign(d, {
                        ...oldFileItem,
                        secretLevel,
                        secretLevelName,
                        filename,
                        ...fileUrl2 && {fileUrl: fileUrl2} //流版签修改附件会把文件的id替换，还原为fileUrl2
                    });
                }

                //附件
                if (d.type === '0') {
                    attachmentList.push(d);
                } else {
                    //关联文档
                    relationDocsList.push(d);
                }
            });
            //attActionLog.editAtt
            useBaseInfo.attachments.atts = attachmentList;
            useBaseInfo.relationDocs.atts = relationDocsList;
            useBaseInfo.attachmentLogs.push(...logList);
            emitter.publish('ModifyAtta');
            dialog.close();
        },
    });
}
export default function (btn) {
    return new Promise((resolve) => {
        updateAttachment.call(this, btn);
        resolve();
    });
}
